package lev01_beginnerClass.class07_linkedLIst;

public class L04_StackWithLinkedList {
    public static class Node<V> {
        public V value;
        public Node next;

        public Node(V v) {
            value = v;
            next = null;
        }
    }

    public static class MyStack<V>{
        Node<V> head;
        int size;

        //构造器
        public MyStack(){
            head = null;
            size = 0;
        }

        //大小
        public int getSize(){
            return  size;
        }

        //是否为空
        public boolean isEmpty(){
            return  size == 0;
        }

        //添加节点 push
        public void push(V value){
            Node<V> cur = new Node<>(value);
            if(head == null){
                head = cur;
            }else {
                cur.next = head;
                head =cur;
            }
        }

        //弹出节点
        public V pop(){
            V ans = null;
            if(head != null){
                ans = head.value;
                head = head.next;
                size--;
            }
            return ans;
        }
        //获取栈顶的值

        public V peek(){
            return head == null? null:head.value;
        }
    }
}
